#!/usr/bin/env python3.8
# -*- encoding: utf-8 -*-
'''
@File    :   views.py
@Time    :   2021/10/09 16:10:01
@Author  :   windstarry 
@Version :   1.0
'''
# here put the import lib

from wtforms import form
from app.auth import auth_bp
from app.models import User,Role
from flask import render_template,redirect,url_for,flash, request
from app.auth.forms import LoginForm,RegistrationForm
from app.auth.email import send_confirm_email
from flask_login import login_user, logout_user,login_required,current_user
from app.ext import db
from app.utils import is_safe_url

@auth_bp.route('/logout')
@login_required
def logout():
    logout_user()
    flash('退出登录', 'success')
    return redirect(url_for('main.index'))


@auth_bp.route('/login', methods=['GET', 'POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user and user.check_password(form.password.data):
            login_user(user, remember=form.remember_me.data)
            next = request.args.get('next')
            flash('登录成功！', 'success')
            if next:  # 验证有没有next
                if is_safe_url(next):
                    return redirect(next)
            return redirect(url_for('main.index'))
        flash('用户名或密码不正确', 'warning')
    return render_template('auth/login.html', title='登录', form=form)



@auth_bp.route('/register',methods=['GET', 'POST'])
def register():
    if current_user.is_authenticated:
        return redirect(url_for('main.index'))
    form = RegistrationForm()
    #当form被提交时执行
    if form.validate_on_submit():
        # 获取表单信息，设置默认注册用户为普通用户
        user = User(username=form.username.data, email=form.email.data, role = Role.query.filter_by(name='User').first())
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('注册成功', 'success')
        # token = user.generate_confirmation_token()
        send_confirm_email(user)
        return redirect(url_for('auth.login'))
    return render_template('auth/register.html',title='用户注册',form=form)


@auth_bp.route('/unconfirmed')
@login_required
def unconfirmed():
    if current_user.confirmed:
        return redirect(url_for('main.index'))
    return render_template('auth/unconfirmed.html',title='账号认证')


@auth_bp.route('/confirm/<token>')
@login_required
def confirm_user(token):
    user = current_user._get_current_object()
    if user.confirm(token):
        db.session.commit()
        flash('验证用户成功！', 'success')
        return redirect(url_for('main.index'))
    flash('令牌不正确，验证失败', 'error')
    return redirect(url_for('main.index'))


@auth_bp.route('/confirm/re-send')
@login_required
def re_send_confirm():
    user = current_user._get_current_object()
    # token = user.generate_confirmation_token()
    send_confirm_email(user)
    flash('一封新的验证邮件发送成功！', 'success')
    return redirect(url_for('main.index'))

